{% extends 'layout_blog.html' %}
{% load comments %}

{% block page_title %}{% if post.title %}{{ post.title }}{% else %}New blog post{% endif %}{% endblock %}

{% block scripts %}
  {{ block.super }}
  {% if can_post %}
    <script type="text/javascript" src="/media/js/blog-admin.js" ></script>
  {% endif %}
{% endblock %}

{% block posts %}

  <div class="document">
    <h1><a href="{{ post.get_absolute_url|escape }}">{% if post.title %}{{ post.title }}{% else %}New blog post{% endif %}</a></h1>
    {% include "blog/post_body.html" %}
  </div>

  {% if post.listed %}
    {% get_free_comment_list for blog.Post post.id as comment_list %}

    {% if comment_list %}
    <div class="comments">
    {% for comment in comment_list %}
      {% if comment.is_public %}
      <div class="comment comment-{% cycle odd,even %}" id="c{{ comment.id }}">
	<div>
	  <a id="c{{ comment.id }}" href="#c{{ comment.id }}">#{{ forloop.counter }}</a>
	  <strong>{{ comment.person_name }}</strong> wrote, on {{ comment.submit_date|date:"Y-m-d" }}:
	</div>

	{{ comment.comment|escape|urlizetrunc:40|linebreaks }}

      </div>
      {% endif %}
    {% endfor %}
    </div>
    {% endif %}

    <div class="comment-form">
      {% free_comment_form for blog.Post post.id %}
    </div>
  {% endif %}

  {% if post_form %}
     <div class="comment-form">
       <form action="{{ post.get_absolute_url }}" method="post">
         <p><label for="id_title">Title:</label> {{ post_form.title }}
           <span class="error">{{ post_form.errors.title.0 }}</span>
           </p>
         <p>{{ post_form.listed }}<label for="id_listed">Post is publicly listed</label></p>
         <p><label for="id_content">reStructuredText markup:</label><br />{{ post_form.content }}
           <div class="error">{{ post_form.errors.content.0 }}</div>
           </p>
         <p><input class="submit" type="submit" value="Save Post" /></p>

         <p class="image-upload">
           <iframe src="/images/upload/" name="image_iframe" id="image_iframe"
                   width="100%" height="0px" frameborder="0"
                   scrolling="no" marginheight="0" marginwidth="0"
                   > </iframe>
         </p>
       </form>
     </div>
  {% endif %}

{% endblock %}

{% block scripts-init %}
  {{ block.super }}
  {% if post_form %}
    iFrameResultCallbacks["image_iframe"] = function(result) {
      if (result) {
        var content = document.getElementById("id_content");
        content.value += "\n.. image:: #"+result+"\n";
      }
    }
  {% endif %}
{% endblock %}

